package com.dao.impl;


import com.dao.MemberService;
import com.entity.Member;
import com.util.JdbcUtil;
import com.util.JsonUtil;
import com.util.PageUtil;
import sun.security.provider.MD5;

import javax.servlet.annotation.WebServlet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2022-03-23
 */
public class MemberServiceImpl implements MemberService {

    @Override
    public int insert(Member member) throws SQLException {

        String sql = "insert into member (nickname,phone,email,sex,avater,password,sign,birthday,localtion,is_disable,is_deleted,create_time,update_time) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] values = new Object[]{member.getNickname(),member.getPhone(),member.getEmail(),member.getSex(),member.getAvater(),
                                        member.getPassword(),member.getSign(),member.getBirthday(),member.getLocaltion(),member.getIsDisable(),0,new Date(),new Date()
        };
        return JdbcUtil.insertOrUpdateOrDelete(sql,values);
    }
    //查询出所有记录
    @Override
    public List<Member> queryData(Member member) throws IllegalAccessException, SQLException, InstantiationException {

//        String sql = "select member_id memberId,nickname,phone phone,email,sex,avater,password,sign,birthday,localtion,is_disable isDisable,is_deleted isDeleted,create_time createTime,update_time updateTime from member where nickname = ?";
        String sql = "select * from member where nickname = ? and is_deleted = 0 ";
        return JdbcUtil.queryData(sql,Member.class,member.getNickname());
    }

    @Override
    public int update(Member member) throws SQLException {
        String sql = "update member set nickname = ?,phone = ?,email = ?,sex = ?,avater = ?,sign = ?,birthday = ?,localtion = ?,is_disable = ?,update_time = ? where member_id = ?";
        Object[] values = new Object[]{member.getNickname(),member.getPhone(),member.getEmail(),member.getSex(),member.getAvater(),
                member.getSign(),member.getBirthday(),member.getLocaltion(),member.getIsDisable(),new Date(),member.getMemberId()
        };
        return JdbcUtil.insertOrUpdateOrDelete(sql,values);
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        String sql = "update member set is_deleted = 1 ,update_time = ? where member_id = ?";

        return JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{new Date(),id});
    }

    public int deleteByIdS(List<Object> obj) throws SQLException{
        String sql = "update member set is_deleted = 1 ,update_time = ? where member_id in (";
        for (int i = 0; i < obj.size();i++) {
            sql += "?,";
        }
        String substring = sql.substring(0,sql.length()-1);
        substring = substring + ")";
        obj.add(0,new Date());
        return JdbcUtil.insertOrUpdateOrDelete(substring,obj.toArray());
    }

    @Override
    public int resetPassword(int id) throws SQLException {
        String sql = "update member set password = 123456 where member_id = ?";

        return JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{id});
    }

    /**
     * 查询密码
     * @param member
     * @return
     */
    @Override
    public List<Member> queryPassword(Member member) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "select * from member where member_id = ?";
        return JdbcUtil.queryData(sql,Member.class,member.getMemberId());
    }


    // 分页查询
    @Override
    public PageUtil<Member> queryDataBypage(int currentPgge, int pageSize, Member member) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "select * from member where is_deleted=0 ";
        ArrayList values = new ArrayList();
        if (member != null){
            if (member.getNickname() != null && !"".equals(member.getNickname())){
                sql += "and nickname like ? ";
                values.add( "%"+member.getNickname() +"%");
            }
        }
        sql += " order by member_id DESC";

        return JdbcUtil.queryDataByPage(sql,Member.class,currentPgge,pageSize,values.toArray());
    }
}
